import 'dart:math';
import 'package:flutter/material.dart';

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class Box extends StatefulWidget {
  const Box({super.key, required this.text});
  final String text;
  @override
  State<Box> createState() => _BoxState();
}

class _BoxState extends State<Box> {
  var r = Random().nextInt(256);
  var g = Random().nextInt(256);
  var b = Random().nextInt(256);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      color: Color.fromRGBO(r, g, b, 1),
      child: Text(
        widget.text,
        style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
      ),
    );
  }
}

class _MyAppState extends State<MyApp> {
  var children = [const Box(text: 'one'), const Box(text: 'two')];
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: SafeArea(
          child: Column(
        children: children,
      )),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: ElevatedButton(
        onPressed: () {
          setState(() {
            children.insert(0, children.removeLast());
          });
        },
        child: const Text('交换'),
      ),
    ));
  }
}

